#!/bin/sh
#
# Build all the dependency packages
#
# http://maxwit.googlecode.com
#
# Authors:
#     Conke Hu    <conke.hu@gmail.com>
#     Tiger Yu    <tigerflying.yu@gmail.com>
#     Fleya Hou   <fleya.hou@gmail.com>
#


THIS_TOP=`dirname $0`

. ${THIS_TOP}/../core/bmw_base

PATCH_PATH=${THIS_TOP}/patch

MWEMU=${UTILS_PATH}/bin/mwemu


echo -n "removing ${UTILS_PATH} ... "
rm -rf ${UTILS_PATH}
echo "OK."

mkdir -vp ${BUILD_PATH}
mkdir -vp ${UTILS_PATH}/bin
mkdir -vp ${UTILS_PATH}/usr/lib
mkdir -vp ${UTILS_PATH}/usr/share/man/man1
echo


# LOG_PATH=${BUILD_PATH}/log

if [ "${BUILD_ARCH}" = x86_64 ]; then
	LIB_CFLAGS="-fPIC"
fi 


APP0_LIST="\
	${MWP_PKGCONFIG} \
	"

LIB_LIST="\
	${MWP_NCURSES} \
	"

APP1_LIST=" \
	${MWP_DEBUTILS} \
	${MWP_BASH} \
	${MWP_COREUTILS} \
	${MWP_GREP} \
	${MWP_M4} \
	${MWP_LIBTOOL} \
	${MWP_FLEX} \
	${MWP_BISON} \
	${MWP_SED} \
	${MWP_GAWK} \
	${MWP_GETTEXT} \
	${MWP_TEXINFO} \
	${MWP_AUTOCONF} \
	${MWP_AUTOMAKE} \
	"


for pkg in ${MWP_PATCH} ${MWP_ZLIB}
do
	Unpack ${pkg}
 	cd ${BUILD_PATH}/${pkg}

	./configure --prefix=${UTILS_PATH}/usr || exit 1

	make && make install || exit 1
	echo
done


for pkg in ${APP0_LIST}
do
	Unpack ${pkg}

	cd ${BUILD_PATH}/${pkg}

	./configure --prefix=/usr || exit 1

	make || exit 1

	make DESTDIR=${UTILS_PATH} install || exit 1

	echo
done

for pkg in ${LIB_LIST}
do
	Unpack ${pkg}

	cd ${BUILD_PATH}/${pkg}

	# LDFLAGS="-L${UTILS_PATH}/lib" \
	CFLAGS="${LIB_CFLAGS}" \
	./configure \
		--prefix=/usr \
		--disable-shared \
		--enable-static \
		--without-cxx \
		|| exit 1

	make -j6 DESTDIR=${UTILS_PATH} install || exit 1
	echo
done


Unpack ${MWP_PERL}
./Configure \
	-des \
	-Dprefix=${UTILS_PATH}/usr \
	|| exit 1	

make -j6 && make install || exit 1
echo


for pkg in ${APP1_LIST}
do
	Unpack ${pkg}

	cd ${BUILD_PATH}/${pkg}

	LDFLAGS="-L${UTILS_PATH}/usr/lib" \
	./configure --prefix=${UTILS_PATH} || exit 1

	make -j6 && make install || exit 1
	echo
done

sed -i "s:\(.*my.*ENV.*\)libtoolize:\1${UTILS_PATH}/bin/libtoolize:" ${UTILS_PATH}/bin/autoreconf || exit 1

echo "Building ${MWP_SBOX2} ..."
Unpack ${MWP_SBOX2}
./autogen.sh && \
make install prefix=${UTILS_PATH} || exit 1
echo


# fixme
echo "Building ${MWP_QEMU}: "
Unpack ${MWP_QEMU}
./configure \
	--prefix=${UTILS_PATH} \
	--target-list=${QEMU_ARCH}-linux-user \
	--cc="gcc -I${UTILS_PATH}/usr/include -L${UTILS_PATH}/usr/lib" \
	--disable-kvm \
	--disable-sdl \
	--disable-gfx-check \
	|| exit 1

make || exit 1
make install || exit 1
echo


# fixme !!
cat > ${MWEMU} << EOF
#!/bin/sh

QEMU_CONSOLE=0
QEMU_MACH="realview"

if [ x"\${QEMU_CONSOLE}" = x"1" ]; then
	EXTRA_QEMU_OPT="-serial stdio"
	EXTRA_COMMAND_LINE="console=ttyAMA0" # fixme
fi

qemu-system-${TARGET_ARCH} -M \${QEMU_MACH} -kernel ${IMAGES_PATH}/zImage -net nic -net tap \${EXTRA_QEMU_OPT} -append "root=/dev/nfs rw nfsroot=\${QEMU_HOST_IP}:${ROOTFS_PATH} ip=\${QEMU_MACH_IP}:\${QEMU_HOST_IP}:\${QEMU_HOST_IP}:255.255.255.0:maxwit.googlecode.com:eth0:off \${EXTRA_COMMAND_LINE}"

EOF

chmod a+x ${MWEMU}


echo
echo "Done."
echo

